<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.db.backends.schema &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../../index.html" />
    <link rel="up" title="django.db" href="../../db.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-db-backends-schema">
            
  <h1>Source code for django.db.backends.schema</h1><div class="highlight"><pre>
<span class="kn">import</span> <span class="nn">hashlib</span>
<span class="kn">import</span> <span class="nn">operator</span>

<span class="kn">from</span> <span class="nn">django.db.backends.creation</span> <span class="kn">import</span> <span class="n">BaseDatabaseCreation</span>
<span class="kn">from</span> <span class="nn">django.db.backends.utils</span> <span class="kn">import</span> <span class="n">truncate_name</span>
<span class="kn">from</span> <span class="nn">django.db.models.fields.related</span> <span class="kn">import</span> <span class="n">ManyToManyField</span>
<span class="kn">from</span> <span class="nn">django.db.transaction</span> <span class="kn">import</span> <span class="n">atomic</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_bytes</span>
<span class="kn">from</span> <span class="nn">django.utils.log</span> <span class="kn">import</span> <span class="n">getLogger</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves</span> <span class="kn">import</span> <span class="nb">reduce</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">getLogger</span><span class="p">(</span><span class="s">&#39;django.db.backends.schema&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="BaseDatabaseSchemaEditor"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor">[docs]</a><span class="k">class</span> <span class="nc">BaseDatabaseSchemaEditor</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class (and its subclasses) are responsible for emitting schema-changing</span>
<span class="sd">    statements to the databases - model creation/removal/alteration, field</span>
<span class="sd">    renaming, index fiddling, and so on.</span>

<span class="sd">    It is intended to eventually completely replace DatabaseCreation.</span>

<span class="sd">    This class should be used by creating an instance for each set of schema</span>
<span class="sd">    changes (e.g. a syncdb run, a migration file), and by first calling start(),</span>
<span class="sd">    then the relevant actions, and then commit(). This is necessary to allow</span>
<span class="sd">    things like circular foreign key references - FKs will only be created once</span>
<span class="sd">    commit() is called.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c"># Overrideable SQL templates</span>
    <span class="n">sql_create_table</span> <span class="o">=</span> <span class="s">&quot;CREATE TABLE </span><span class="si">%(table)s</span><span class="s"> (</span><span class="si">%(definition)s</span><span class="s">)&quot;</span>
    <span class="n">sql_create_table_unique</span> <span class="o">=</span> <span class="s">&quot;UNIQUE (</span><span class="si">%(columns)s</span><span class="s">)&quot;</span>
    <span class="n">sql_rename_table</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(old_table)s</span><span class="s"> RENAME TO </span><span class="si">%(new_table)s</span><span class="s">&quot;</span>
    <span class="n">sql_retablespace_table</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> SET TABLESPACE </span><span class="si">%(new_tablespace)s</span><span class="s">&quot;</span>
    <span class="n">sql_delete_table</span> <span class="o">=</span> <span class="s">&quot;DROP TABLE </span><span class="si">%(table)s</span><span class="s"> CASCADE&quot;</span>

    <span class="n">sql_create_column</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> ADD COLUMN </span><span class="si">%(column)s</span><span class="s"> </span><span class="si">%(definition)s</span><span class="s">&quot;</span>
    <span class="n">sql_alter_column</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> </span><span class="si">%(changes)s</span><span class="s">&quot;</span>
    <span class="n">sql_alter_column_type</span> <span class="o">=</span> <span class="s">&quot;ALTER COLUMN </span><span class="si">%(column)s</span><span class="s"> TYPE </span><span class="si">%(type)s</span><span class="s">&quot;</span>
    <span class="n">sql_alter_column_null</span> <span class="o">=</span> <span class="s">&quot;ALTER COLUMN </span><span class="si">%(column)s</span><span class="s"> DROP NOT NULL&quot;</span>
    <span class="n">sql_alter_column_not_null</span> <span class="o">=</span> <span class="s">&quot;ALTER COLUMN </span><span class="si">%(column)s</span><span class="s"> SET NOT NULL&quot;</span>
    <span class="n">sql_alter_column_default</span> <span class="o">=</span> <span class="s">&quot;ALTER COLUMN </span><span class="si">%(column)s</span><span class="s"> SET DEFAULT </span><span class="si">%(default)s</span><span class="s">&quot;</span>
    <span class="n">sql_alter_column_no_default</span> <span class="o">=</span> <span class="s">&quot;ALTER COLUMN </span><span class="si">%(column)s</span><span class="s"> DROP DEFAULT&quot;</span>
    <span class="n">sql_delete_column</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> DROP COLUMN </span><span class="si">%(column)s</span><span class="s"> CASCADE&quot;</span>
    <span class="n">sql_rename_column</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> RENAME COLUMN </span><span class="si">%(old_column)s</span><span class="s"> TO </span><span class="si">%(new_column)s</span><span class="s">&quot;</span>
    <span class="n">sql_update_with_default</span> <span class="o">=</span> <span class="s">&quot;UPDATE </span><span class="si">%(table)s</span><span class="s"> SET </span><span class="si">%(column)s</span><span class="s"> = </span><span class="si">%(default)s</span><span class="s"> WHERE </span><span class="si">%(column)s</span><span class="s"> IS NULL&quot;</span>

    <span class="n">sql_create_check</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> ADD CONSTRAINT </span><span class="si">%(name)s</span><span class="s"> CHECK (</span><span class="si">%(check)s</span><span class="s">)&quot;</span>
    <span class="n">sql_delete_check</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> DROP CONSTRAINT </span><span class="si">%(name)s</span><span class="s">&quot;</span>

    <span class="n">sql_create_unique</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> ADD CONSTRAINT </span><span class="si">%(name)s</span><span class="s"> UNIQUE (</span><span class="si">%(columns)s</span><span class="s">)&quot;</span>
    <span class="n">sql_delete_unique</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> DROP CONSTRAINT </span><span class="si">%(name)s</span><span class="s">&quot;</span>

    <span class="n">sql_create_fk</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> ADD CONSTRAINT </span><span class="si">%(name)s</span><span class="s"> FOREIGN KEY (</span><span class="si">%(column)s</span><span class="s">) REFERENCES </span><span class="si">%(to_table)s</span><span class="s"> (</span><span class="si">%(to_column)s</span><span class="s">) DEFERRABLE INITIALLY DEFERRED&quot;</span>
    <span class="n">sql_create_inline_fk</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">sql_delete_fk</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> DROP CONSTRAINT </span><span class="si">%(name)s</span><span class="s">&quot;</span>

    <span class="n">sql_create_index</span> <span class="o">=</span> <span class="s">&quot;CREATE INDEX </span><span class="si">%(name)s</span><span class="s"> ON </span><span class="si">%(table)s</span><span class="s"> (</span><span class="si">%(columns)s</span><span class="s">)</span><span class="si">%(extra)s</span><span class="s">&quot;</span>
    <span class="n">sql_delete_index</span> <span class="o">=</span> <span class="s">&quot;DROP INDEX </span><span class="si">%(name)s</span><span class="s">&quot;</span>

    <span class="n">sql_create_pk</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> ADD CONSTRAINT </span><span class="si">%(name)s</span><span class="s"> PRIMARY KEY (</span><span class="si">%(columns)s</span><span class="s">)&quot;</span>
    <span class="n">sql_delete_pk</span> <span class="o">=</span> <span class="s">&quot;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> DROP CONSTRAINT </span><span class="si">%(name)s</span><span class="s">&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="n">collect_sql</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connection</span> <span class="o">=</span> <span class="n">connection</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">collect_sql</span> <span class="o">=</span> <span class="n">collect_sql</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">collect_sql</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">collected_sql</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="c"># State-managing methods</span>

    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">can_rollback_ddl</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">atomic</span> <span class="o">=</span> <span class="n">atomic</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">atomic</span><span class="o">.</span><span class="n">__enter__</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">exc_type</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">sql</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">can_rollback_ddl</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">atomic</span><span class="o">.</span><span class="n">__exit__</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">)</span>

    <span class="c"># Core utility functions</span>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.execute"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="p">[]):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Executes the given SQL statement, with optional parameters.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Log the command we&#39;re running, then run it</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">; (params </span><span class="si">%r</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">))</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">collect_sql</span><span class="p">:</span>
            <span class="n">ending</span> <span class="o">=</span> <span class="s">&quot;&quot;</span> <span class="k">if</span> <span class="n">sql</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">&quot;;&quot;</span><span class="p">)</span> <span class="k">else</span> <span class="s">&quot;;&quot;</span>
            <span class="k">if</span> <span class="n">params</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">collected_sql</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">sql</span> <span class="o">%</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quote_value</span><span class="p">,</span> <span class="n">params</span><span class="p">)))</span> <span class="o">+</span> <span class="n">ending</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">collected_sql</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sql</span> <span class="o">+</span> <span class="n">ending</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span> <span class="k">as</span> <span class="n">cursor</span><span class="p">:</span>
                <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
</div>
    <span class="k">def</span> <span class="nf">quote_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="c"># Field &lt;-&gt; database mapping functions</span>

    <span class="k">def</span> <span class="nf">column_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">include_default</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Takes a field and returns its column definition.</span>
<span class="sd">        The field must already have had set_attributes_from_name called.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Get the column&#39;s type and use that as the basis of the SQL</span>
        <span class="n">db_params</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="n">db_params</span><span class="p">[</span><span class="s">&#39;type&#39;</span><span class="p">]</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c"># Check for fields that aren&#39;t actually columns (e.g. M2M)</span>
        <span class="k">if</span> <span class="n">sql</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">None</span><span class="p">,</span> <span class="bp">None</span>
        <span class="c"># Work out nullability</span>
        <span class="n">null</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">null</span>
        <span class="c"># If we were told to include a default value, do so</span>
        <span class="n">default_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">effective_default</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
        <span class="n">include_default</span> <span class="o">=</span> <span class="n">include_default</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">skip_default</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">include_default</span> <span class="ow">and</span> <span class="n">default_value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">requires_literal_defaults</span><span class="p">:</span>
                <span class="c"># Some databases can&#39;t take defaults as a parameter (oracle)</span>
                <span class="c"># If this is the case, the individual schema backend should</span>
                <span class="c"># implement prepare_default</span>
                <span class="n">sql</span> <span class="o">+=</span> <span class="s">&quot; DEFAULT </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">prepare_default</span><span class="p">(</span><span class="n">default_value</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">sql</span> <span class="o">+=</span> <span class="s">&quot; DEFAULT </span><span class="si">%s</span><span class="s">&quot;</span>
                <span class="n">params</span> <span class="o">+=</span> <span class="p">[</span><span class="n">default_value</span><span class="p">]</span>
        <span class="c"># Oracle treats the empty string (&#39;&#39;) as null, so coerce the null</span>
        <span class="c"># option whenever &#39;&#39; is a possible value.</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">empty_strings_allowed</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">interprets_empty_strings_as_nulls</span><span class="p">):</span>
            <span class="n">null</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="k">if</span> <span class="n">null</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">implied_column_null</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">+=</span> <span class="s">&quot; NULL&quot;</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">null</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">+=</span> <span class="s">&quot; NOT NULL&quot;</span>
        <span class="c"># Primary key/unique outputs</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">+=</span> <span class="s">&quot; PRIMARY KEY&quot;</span>
        <span class="k">elif</span> <span class="n">field</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">+=</span> <span class="s">&quot; UNIQUE&quot;</span>
        <span class="c"># Optionally add the tablespace if it&#39;s an implicitly indexed column</span>
        <span class="n">tablespace</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">db_tablespace</span> <span class="ow">or</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span>
        <span class="k">if</span> <span class="n">tablespace</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_tablespaces</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">+=</span> <span class="s">&quot; </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">tablespace_sql</span><span class="p">(</span><span class="n">tablespace</span><span class="p">,</span> <span class="n">inline</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="c"># Return the sql</span>
        <span class="k">return</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span>

    <span class="k">def</span> <span class="nf">skip_default</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Some backends don&#39;t accept default values for certain columns types</span>
<span class="sd">        (i.e. MySQL longtext and longblob).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">False</span>

    <span class="k">def</span> <span class="nf">prepare_default</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Only used for backends which have requires_literal_defaults feature</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s">&#39;subclasses of BaseDatabaseSchemaEditor for backends which have requires_literal_defaults must provide a prepare_default() method&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">effective_default</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a field&#39;s effective database default value</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">has_default</span><span class="p">():</span>
            <span class="n">default</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_default</span><span class="p">()</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">null</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">blank</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">empty_strings_allowed</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">get_internal_type</span><span class="p">()</span> <span class="o">==</span> <span class="s">&quot;BinaryField&quot;</span><span class="p">:</span>
                <span class="n">default</span> <span class="o">=</span> <span class="n">six</span><span class="o">.</span><span class="n">binary_type</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">default</span> <span class="o">=</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">default</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="c"># If it&#39;s a callable, call it</span>
        <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">callable</span><span class="p">(</span><span class="n">default</span><span class="p">):</span>
            <span class="n">default</span> <span class="o">=</span> <span class="n">default</span><span class="p">()</span>
        <span class="c"># Run it through the field&#39;s get_db_prep_save method so we can send it</span>
        <span class="c"># to the database.</span>
        <span class="n">default</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_db_prep_save</span><span class="p">(</span><span class="n">default</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">default</span>

    <span class="k">def</span> <span class="nf">quote_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a quoted version of the value so it&#39;s safe to use in an SQL</span>
<span class="sd">        string. This is not safe against injection from user code; it is</span>
<span class="sd">        intended only for use in making SQL scripts or preparing default values</span>
<span class="sd">        for particularly tricky backends (defaults are not user-defined, though,</span>
<span class="sd">        so this is safe).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>

    <span class="c"># Actions</span>

<div class="viewcode-block" id="BaseDatabaseSchemaEditor.create_model"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor.create_model">[docs]</a>    <span class="k">def</span> <span class="nf">create_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Takes a model and creates a table for it in the database.</span>
<span class="sd">        Will also create any accompanying indexes or unique constraints.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Create column SQL, add FK deferreds if needed</span>
        <span class="n">column_sqls</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
            <span class="c"># SQL</span>
            <span class="n">definition</span><span class="p">,</span> <span class="n">extra_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">column_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">definition</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="c"># Check constraints can go on the column SQL here</span>
            <span class="n">db_params</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]:</span>
                <span class="n">definition</span> <span class="o">+=</span> <span class="s">&quot; CHECK (</span><span class="si">%s</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="n">db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]</span>
            <span class="c"># Autoincrement SQL (for backends with inline variant)</span>
            <span class="n">col_type_suffix</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">db_type_suffix</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">col_type_suffix</span><span class="p">:</span>
                <span class="n">definition</span> <span class="o">+=</span> <span class="s">&quot; </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">col_type_suffix</span>
            <span class="n">params</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">extra_params</span><span class="p">)</span>
            <span class="c"># FK</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">:</span>
                <span class="n">to_table</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span>
                <span class="n">to_column</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">field_name</span><span class="p">)</span><span class="o">.</span><span class="n">column</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_foreign_keys</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_fk_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="s">&quot;_fk_</span><span class="si">%(to_table)s</span><span class="s">_</span><span class="si">%(to_column)s</span><span class="s">&quot;</span><span class="p">))</span>
                <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_inline_fk</span><span class="p">:</span>
                    <span class="n">definition</span> <span class="o">+=</span> <span class="s">&quot; &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_inline_fk</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s">&quot;to_table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">to_table</span><span class="p">),</span>
                        <span class="s">&quot;to_column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">to_column</span><span class="p">),</span>
                    <span class="p">}</span>
            <span class="c"># Add the SQL to our big list</span>
            <span class="n">column_sqls</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                <span class="n">definition</span><span class="p">,</span>
            <span class="p">))</span>
            <span class="c"># Autoincrement SQL (for backends with post table definition variant)</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">get_internal_type</span><span class="p">()</span> <span class="o">==</span> <span class="s">&quot;AutoField&quot;</span><span class="p">:</span>
                <span class="n">autoinc_sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">autoinc_sql</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">autoinc_sql</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">autoinc_sql</span><span class="p">)</span>
        <span class="c"># Add any unique_togethers</span>
        <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">unique_together</span><span class="p">:</span>
            <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">field</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">column</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">]</span>
            <span class="n">column_sqls</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_create_table_unique</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s">&quot;columns&quot;</span><span class="p">:</span> <span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">),</span>
            <span class="p">})</span>
        <span class="c"># Make the table</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_table</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s">&quot;definition&quot;</span><span class="p">:</span> <span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">column_sqls</span><span class="p">)</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">:</span>
            <span class="n">tablespace_sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">tablespace_sql</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">tablespace_sql</span><span class="p">:</span>
                <span class="n">sql</span> <span class="o">+=</span> <span class="s">&#39; &#39;</span> <span class="o">+</span> <span class="n">tablespace_sql</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>

        <span class="c"># Add any field index and index_together&#39;s (deferred as SQLite3 _remake_table needs it)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_model_indexes_sql</span><span class="p">(</span><span class="n">model</span><span class="p">))</span>

        <span class="c"># Make M2M tables</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">create_model</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="BaseDatabaseSchemaEditor.delete_model"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor.delete_model">[docs]</a>    <span class="k">def</span> <span class="nf">delete_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Deletes a model from the database.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Handle auto-created intermediary models</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">delete_model</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">)</span>

        <span class="c"># Delete the table</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_table</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
        <span class="p">})</span>
</div>
<div class="viewcode-block" id="BaseDatabaseSchemaEditor.alter_unique_together"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor.alter_unique_together">[docs]</a>    <span class="k">def</span> <span class="nf">alter_unique_together</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_unique_together</span><span class="p">,</span> <span class="n">new_unique_together</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Deals with a model changing its unique_together.</span>
<span class="sd">        Note: The input unique_togethers must be doubly-nested, not the single-</span>
<span class="sd">        nested [&quot;foo&quot;, &quot;bar&quot;] format.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">olds</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">old_unique_together</span><span class="p">)</span>
        <span class="n">news</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">new_unique_together</span><span class="p">)</span>
        <span class="c"># Deleted uniques</span>
        <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">olds</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">news</span><span class="p">):</span>
            <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">field</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">column</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">]</span>
            <span class="n">constraint_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Found wrong number (</span><span class="si">%s</span><span class="s">) of constraints for </span><span class="si">%s</span><span class="s">(</span><span class="si">%s</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">),</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                    <span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">columns</span><span class="p">),</span>
                <span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_unique</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">constraint_names</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
        <span class="c"># Created uniques</span>
        <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">news</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">olds</span><span class="p">):</span>
            <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">field</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">column</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_unique_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">columns</span><span class="p">))</span>
</div>
<div class="viewcode-block" id="BaseDatabaseSchemaEditor.alter_index_together"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor.alter_index_together">[docs]</a>    <span class="k">def</span> <span class="nf">alter_index_together</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_index_together</span><span class="p">,</span> <span class="n">new_index_together</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Deals with a model changing its index_together.</span>
<span class="sd">        Note: The input index_togethers must be doubly-nested, not the single-</span>
<span class="sd">        nested [&quot;foo&quot;, &quot;bar&quot;] format.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">olds</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">old_index_together</span><span class="p">)</span>
        <span class="n">news</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">new_index_together</span><span class="p">)</span>
        <span class="c"># Deleted indexes</span>
        <span class="k">for</span> <span class="n">fields</span> <span class="ow">in</span> <span class="n">olds</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">news</span><span class="p">):</span>
            <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">field</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">column</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">]</span>
            <span class="n">constraint_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">columns</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Found wrong number (</span><span class="si">%s</span><span class="s">) of constraints for </span><span class="si">%s</span><span class="s">(</span><span class="si">%s</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">),</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                    <span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">columns</span><span class="p">),</span>
                <span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_index</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">constraint_names</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
        <span class="c"># Created indexes</span>
        <span class="k">for</span> <span class="n">field_names</span> <span class="ow">in</span> <span class="n">news</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">olds</span><span class="p">):</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">field</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">field_names</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s">&quot;_idx&quot;</span><span class="p">))</span>
</div>
<div class="viewcode-block" id="BaseDatabaseSchemaEditor.alter_db_table"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor.alter_db_table">[docs]</a>    <span class="k">def</span> <span class="nf">alter_db_table</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_db_table</span><span class="p">,</span> <span class="n">new_db_table</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Renames the table a model points to.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">old_db_table</span> <span class="o">==</span> <span class="n">new_db_table</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_rename_table</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;old_table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">old_db_table</span><span class="p">),</span>
            <span class="s">&quot;new_table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_db_table</span><span class="p">),</span>
        <span class="p">})</span>
</div>
<div class="viewcode-block" id="BaseDatabaseSchemaEditor.alter_db_tablespace"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor.alter_db_tablespace">[docs]</a>    <span class="k">def</span> <span class="nf">alter_db_tablespace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_db_tablespace</span><span class="p">,</span> <span class="n">new_db_tablespace</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Moves a model&#39;s table between tablespaces</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_retablespace_table</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s">&quot;old_tablespace&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">old_db_tablespace</span><span class="p">),</span>
            <span class="s">&quot;new_tablespace&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_db_tablespace</span><span class="p">),</span>
        <span class="p">})</span>
</div>
<div class="viewcode-block" id="BaseDatabaseSchemaEditor.add_field"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor.add_field">[docs]</a>    <span class="k">def</span> <span class="nf">add_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Creates a field on a model.</span>
<span class="sd">        Usually involves adding a column, but may involve adding a</span>
<span class="sd">        table instead (for M2M fields)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Special-case implicit M2M tables</span>
        <span class="k">if</span> <span class="p">((</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">ManyToManyField</span><span class="p">)</span> <span class="ow">or</span> <span class="n">field</span><span class="o">.</span><span class="n">get_internal_type</span><span class="p">()</span> <span class="o">==</span> <span class="s">&#39;ManyToManyField&#39;</span><span class="p">)</span> <span class="ow">and</span>
                <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_model</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">)</span>
        <span class="c"># Get the column&#39;s definition</span>
        <span class="n">definition</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">column_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">include_default</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="c"># It might not actually have a column behind it</span>
        <span class="k">if</span> <span class="n">definition</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="c"># Check constraints can go on the column SQL here</span>
        <span class="n">db_params</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]:</span>
            <span class="n">definition</span> <span class="o">+=</span> <span class="s">&quot; CHECK (</span><span class="si">%s</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="n">db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]</span>
        <span class="c"># Build the SQL and run it</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_column</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
            <span class="s">&quot;definition&quot;</span><span class="p">:</span> <span class="n">definition</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="c"># Drop the default if we need to</span>
        <span class="c"># (Django usually does not use in-database defaults)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">skip_default</span><span class="p">(</span><span class="n">field</span><span class="p">)</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">default</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                <span class="s">&quot;changes&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_no_default</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                <span class="p">}</span>
            <span class="p">}</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
        <span class="c"># Add an index, if required</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="p">]))</span>
        <span class="c"># Add any FK constraints later</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_foreign_keys</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">deferred_sql</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_fk_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="s">&quot;_fk_</span><span class="si">%(to_table)s</span><span class="s">_</span><span class="si">%(to_column)s</span><span class="s">&quot;</span><span class="p">))</span>
        <span class="c"># Reset connection if required</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">connection_persists_old_columns</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="BaseDatabaseSchemaEditor.remove_field"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor.remove_field">[docs]</a>    <span class="k">def</span> <span class="nf">remove_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Removes a field from a model. Usually involves deleting a column,</span>
<span class="sd">        but for M2Ms may involve deleting a table.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Special-case implicit M2M tables</span>
        <span class="k">if</span> <span class="p">((</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">ManyToManyField</span><span class="p">)</span> <span class="ow">or</span> <span class="n">field</span><span class="o">.</span><span class="n">get_internal_type</span><span class="p">()</span> <span class="o">==</span> <span class="s">&#39;ManyToManyField&#39;</span><span class="p">)</span> <span class="ow">and</span>
                <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">delete_model</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">)</span>
        <span class="c"># It might not actually have a column behind it</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)[</span><span class="s">&#39;type&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="c"># Drop any FK constraints, MySQL requires explicit deletion</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">rel</span><span class="p">:</span>
            <span class="n">fk_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">foreign_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">fk_name</span> <span class="ow">in</span> <span class="n">fk_names</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_fk</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">fk_name</span><span class="p">))</span>
        <span class="c"># Delete the column</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_column</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
        <span class="c"># Reset connection if required</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">connection_persists_old_columns</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="BaseDatabaseSchemaEditor.alter_field"><a class="viewcode-back" href="../../../../ref/schema-editor.html#django.db.backends.schema.BaseDatabaseSchemaEditor.alter_field">[docs]</a>    <span class="k">def</span> <span class="nf">alter_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Allows a field&#39;s type, uniqueness, nullability, default, column,</span>
<span class="sd">        constraints etc. to be modified.</span>
<span class="sd">        Requires a copy of the old field as well so we can only perform</span>
<span class="sd">        changes that are required.</span>
<span class="sd">        If strict is true, raises errors if the old column does not match old_field precisely.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Ensure this field is even column-based</span>
        <span class="n">old_db_params</span> <span class="o">=</span> <span class="n">old_field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
        <span class="n">old_type</span> <span class="o">=</span> <span class="n">old_db_params</span><span class="p">[</span><span class="s">&#39;type&#39;</span><span class="p">]</span>
        <span class="n">new_db_params</span> <span class="o">=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
        <span class="n">new_type</span> <span class="o">=</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s">&#39;type&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">old_type</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">old_field</span><span class="o">.</span><span class="n">rel</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">new_type</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Cannot alter field </span><span class="si">%s</span><span class="s"> into </span><span class="si">%s</span><span class="s"> - they do not properly define db_type (are you using PostGIS 1.5 or badly-written custom fields?)&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">old_field</span><span class="p">,</span>
                <span class="n">new_field</span><span class="p">,</span>
            <span class="p">))</span>
        <span class="k">elif</span> <span class="n">old_type</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">new_type</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span> <span class="n">old_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_alter_many_to_many</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">strict</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">old_type</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">new_type</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">):</span>
            <span class="c"># Both sides have through models; this is a no-op.</span>
            <span class="k">return</span>
        <span class="k">elif</span> <span class="n">old_type</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="n">new_type</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Cannot alter field </span><span class="si">%s</span><span class="s"> into </span><span class="si">%s</span><span class="s"> - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">old_field</span><span class="p">,</span>
                <span class="n">new_field</span><span class="p">,</span>
            <span class="p">))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_alter_field</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">old_type</span><span class="p">,</span> <span class="n">new_type</span><span class="p">,</span> <span class="n">old_db_params</span><span class="p">,</span> <span class="n">new_db_params</span><span class="p">,</span> <span class="n">strict</span><span class="p">)</span>
</div>
    <span class="k">def</span> <span class="nf">_alter_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">old_type</span><span class="p">,</span> <span class="n">new_type</span><span class="p">,</span> <span class="n">old_db_params</span><span class="p">,</span> <span class="n">new_db_params</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Actually perform a &quot;physical&quot; (non-ManyToMany) field update.&quot;&quot;&quot;</span>

        <span class="c"># Drop any FK constraints, we&#39;ll remake them later</span>
        <span class="n">fks_dropped</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">rel</span> <span class="ow">and</span> <span class="n">old_field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">:</span>
            <span class="n">fk_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">foreign_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">strict</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">fk_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Found wrong number (</span><span class="si">%s</span><span class="s">) of foreign key constraints for </span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="nb">len</span><span class="p">(</span><span class="n">fk_names</span><span class="p">),</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                    <span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">,</span>
                <span class="p">))</span>
            <span class="k">for</span> <span class="n">fk_name</span> <span class="ow">in</span> <span class="n">fk_names</span><span class="p">:</span>
                <span class="n">fks_dropped</span><span class="o">.</span><span class="n">add</span><span class="p">((</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">,))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_fk</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">fk_name</span><span class="p">))</span>
        <span class="c"># Has unique been removed?</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">or</span> <span class="p">(</span><span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">)):</span>
            <span class="c"># Find the unique constraint for this field</span>
            <span class="n">constraint_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">strict</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Found wrong number (</span><span class="si">%s</span><span class="s">) of unique constraints for </span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">),</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                    <span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">,</span>
                <span class="p">))</span>
            <span class="k">for</span> <span class="n">constraint_name</span> <span class="ow">in</span> <span class="n">constraint_names</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_unique</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">constraint_name</span><span class="p">))</span>
        <span class="c"># Drop incoming FK constraints if we&#39;re a primary key and things are going</span>
        <span class="c"># to change.</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">old_type</span> <span class="o">!=</span> <span class="n">new_type</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">rel</span> <span class="ow">in</span> <span class="n">new_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_all_related_objects</span><span class="p">():</span>
                <span class="n">rel_fk_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">rel</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">rel</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">foreign_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">fk_name</span> <span class="ow">in</span> <span class="n">rel_fk_names</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_fk</span><span class="p">,</span> <span class="n">rel</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">fk_name</span><span class="p">))</span>
        <span class="c"># Removed an index? (no strict check, as multiple indexes are possible)</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">and</span> <span class="ow">not</span>
                <span class="p">(</span><span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">and</span> <span class="n">old_field</span><span class="o">.</span><span class="n">unique</span><span class="p">)):</span>
            <span class="c"># Find the index for this field</span>
            <span class="n">index_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">index</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">index_name</span> <span class="ow">in</span> <span class="n">index_names</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_index</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">index_name</span><span class="p">))</span>
        <span class="c"># Change check constraints?</span>
        <span class="k">if</span> <span class="n">old_db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">old_db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]:</span>
            <span class="n">constraint_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">check</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">strict</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Found wrong number (</span><span class="si">%s</span><span class="s">) of check constraints for </span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">),</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                    <span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">,</span>
                <span class="p">))</span>
            <span class="k">for</span> <span class="n">constraint_name</span> <span class="ow">in</span> <span class="n">constraint_names</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_check</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">constraint_name</span><span class="p">))</span>
        <span class="c"># Have they renamed the column?</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">column</span> <span class="o">!=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_rename_column</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                <span class="s">&quot;old_column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                <span class="s">&quot;new_column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                <span class="s">&quot;type&quot;</span><span class="p">:</span> <span class="n">new_type</span><span class="p">,</span>
            <span class="p">})</span>
        <span class="c"># Next, start accumulating actions to do</span>
        <span class="n">actions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">null_actions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">post_actions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c"># Type change?</span>
        <span class="k">if</span> <span class="n">old_type</span> <span class="o">!=</span> <span class="n">new_type</span><span class="p">:</span>
            <span class="n">fragment</span><span class="p">,</span> <span class="n">other_actions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_alter_column_type_sql</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">,</span> <span class="n">new_type</span><span class="p">)</span>
            <span class="n">actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fragment</span><span class="p">)</span>
            <span class="n">post_actions</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">other_actions</span><span class="p">)</span>
        <span class="c"># When changing a column NULL constraint to NOT NULL with a given</span>
        <span class="c"># default value, we need to perform 4 steps:</span>
        <span class="c">#  1. Add a default for new incoming writes</span>
        <span class="c">#  2. Update existing NULL rows with new default</span>
        <span class="c">#  3. Replace NULL constraint with NOT NULL</span>
        <span class="c">#  4. Drop the default again.</span>
        <span class="c"># Default change?</span>
        <span class="n">old_default</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">effective_default</span><span class="p">(</span><span class="n">old_field</span><span class="p">)</span>
        <span class="n">new_default</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">effective_default</span><span class="p">(</span><span class="n">new_field</span><span class="p">)</span>
        <span class="n">needs_database_default</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">old_default</span> <span class="o">!=</span> <span class="n">new_default</span> <span class="ow">and</span>
            <span class="n">new_default</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span>
            <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">skip_default</span><span class="p">(</span><span class="n">new_field</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">needs_database_default</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">requires_literal_defaults</span><span class="p">:</span>
                <span class="c"># Some databases can&#39;t take defaults as a parameter (oracle)</span>
                <span class="c"># If this is the case, the individual schema backend should</span>
                <span class="c"># implement prepare_default</span>
                <span class="n">actions</span><span class="o">.</span><span class="n">append</span><span class="p">((</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_default</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                        <span class="s">&quot;default&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">prepare_default</span><span class="p">(</span><span class="n">new_default</span><span class="p">),</span>
                    <span class="p">},</span>
                    <span class="p">[],</span>
                <span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">actions</span><span class="o">.</span><span class="n">append</span><span class="p">((</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_default</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                        <span class="s">&quot;default&quot;</span><span class="p">:</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span>
                    <span class="p">},</span>
                    <span class="p">[</span><span class="n">new_default</span><span class="p">],</span>
                <span class="p">))</span>
        <span class="c"># Nullability change?</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">null</span> <span class="o">!=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">null</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">new_field</span><span class="o">.</span><span class="n">null</span><span class="p">:</span>
                <span class="n">null_actions</span><span class="o">.</span><span class="n">append</span><span class="p">((</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_null</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                        <span class="s">&quot;type&quot;</span><span class="p">:</span> <span class="n">new_type</span><span class="p">,</span>
                    <span class="p">},</span>
                    <span class="p">[],</span>
                <span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">null_actions</span><span class="o">.</span><span class="n">append</span><span class="p">((</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_not_null</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                        <span class="s">&quot;type&quot;</span><span class="p">:</span> <span class="n">new_type</span><span class="p">,</span>
                    <span class="p">},</span>
                    <span class="p">[],</span>
                <span class="p">))</span>
        <span class="c"># Only if we have a default and there is a change from NULL to NOT NULL</span>
        <span class="n">four_way_default_alteration</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">new_field</span><span class="o">.</span><span class="n">has_default</span><span class="p">()</span> <span class="ow">and</span>
            <span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">null</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">null</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">actions</span> <span class="ow">or</span> <span class="n">null_actions</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">four_way_default_alteration</span><span class="p">:</span>
                <span class="c"># If we don&#39;t have to do a 4-way default alteration we can</span>
                <span class="c"># directly run a (NOT) NULL alteration</span>
                <span class="n">actions</span> <span class="o">=</span> <span class="n">actions</span> <span class="o">+</span> <span class="n">null_actions</span>
            <span class="c"># Combine actions together if we can (e.g. postgres)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_combined_alters</span> <span class="ow">and</span> <span class="n">actions</span><span class="p">:</span>
                <span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">actions</span><span class="p">))</span>
                <span class="n">actions</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">sql</span><span class="p">),</span> <span class="nb">reduce</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">,</span> <span class="n">params</span><span class="p">))]</span>
            <span class="c"># Apply those actions</span>
            <span class="k">for</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="ow">in</span> <span class="n">actions</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                        <span class="s">&quot;changes&quot;</span><span class="p">:</span> <span class="n">sql</span><span class="p">,</span>
                    <span class="p">},</span>
                    <span class="n">params</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="k">if</span> <span class="n">four_way_default_alteration</span><span class="p">:</span>
                <span class="c"># Update existing rows with default value</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">sql_update_with_default</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                        <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                        <span class="s">&quot;default&quot;</span><span class="p">:</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span>
                    <span class="p">},</span>
                    <span class="p">[</span><span class="n">new_default</span><span class="p">],</span>
                <span class="p">)</span>
                <span class="c"># Since we didn&#39;t run a NOT NULL change before we need to do it</span>
                <span class="c"># now</span>
                <span class="k">for</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="ow">in</span> <span class="n">null_actions</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column</span> <span class="o">%</span> <span class="p">{</span>
                            <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                            <span class="s">&quot;changes&quot;</span><span class="p">:</span> <span class="n">sql</span><span class="p">,</span>
                        <span class="p">},</span>
                        <span class="n">params</span><span class="p">,</span>
                    <span class="p">)</span>
        <span class="k">if</span> <span class="n">post_actions</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">sql</span><span class="p">,</span> <span class="n">params</span> <span class="ow">in</span> <span class="n">post_actions</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="c"># Added a unique?</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_unique_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">]))</span>
        <span class="c"># Added an index?</span>
        <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">and</span> <span class="ow">not</span>
                <span class="p">(</span><span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">unique</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">unique</span><span class="p">)):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">new_field</span><span class="p">],</span> <span class="n">suffix</span><span class="o">=</span><span class="s">&quot;_uniq&quot;</span><span class="p">))</span>
        <span class="c"># Type alteration on primary key? Then we need to alter the column</span>
        <span class="c"># referring to us.</span>
        <span class="n">rels_to_update</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">old_type</span> <span class="o">!=</span> <span class="n">new_type</span><span class="p">:</span>
            <span class="n">rels_to_update</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_all_related_objects</span><span class="p">())</span>
        <span class="c"># Changed to become primary key?</span>
        <span class="c"># Note that we don&#39;t detect unsetting of a PK, as we assume another field</span>
        <span class="c"># will always come along and replace it.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">:</span>
            <span class="c"># First, drop the old PK</span>
            <span class="n">constraint_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_constraint_names</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">strict</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Found wrong number (</span><span class="si">%s</span><span class="s">) of PK constraints for </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="nb">len</span><span class="p">(</span><span class="n">constraint_names</span><span class="p">),</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                <span class="p">))</span>
            <span class="k">for</span> <span class="n">constraint_name</span> <span class="ow">in</span> <span class="n">constraint_names</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sql_delete_pk</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">constraint_name</span><span class="p">))</span>
            <span class="c"># Make the new one</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_pk</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                    <span class="s">&quot;name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_name</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">suffix</span><span class="o">=</span><span class="s">&quot;_pk&quot;</span><span class="p">)),</span>
                    <span class="s">&quot;columns&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                <span class="p">}</span>
            <span class="p">)</span>
            <span class="c"># Update all referencing columns</span>
            <span class="n">rels_to_update</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_all_related_objects</span><span class="p">())</span>
        <span class="c"># Handle our type alters on the other end of rels from the PK stuff above</span>
        <span class="k">for</span> <span class="n">rel</span> <span class="ow">in</span> <span class="n">rels_to_update</span><span class="p">:</span>
            <span class="n">rel_db_params</span> <span class="o">=</span> <span class="n">rel</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">db_parameters</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
            <span class="n">rel_type</span> <span class="o">=</span> <span class="n">rel_db_params</span><span class="p">[</span><span class="s">&#39;type&#39;</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">rel</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                    <span class="s">&quot;changes&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_type</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">rel</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                        <span class="s">&quot;type&quot;</span><span class="p">:</span> <span class="n">rel_type</span><span class="p">,</span>
                    <span class="p">}</span>
                <span class="p">}</span>
            <span class="p">)</span>
        <span class="c"># Does it have a foreign key?</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">rel</span> <span class="ow">and</span>
                <span class="p">(</span><span class="n">fks_dropped</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">rel</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">old_field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">)</span> <span class="ow">and</span>
                <span class="n">new_field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_fk_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="s">&quot;_fk_</span><span class="si">%(to_table)s</span><span class="s">_</span><span class="si">%(to_column)s</span><span class="s">&quot;</span><span class="p">))</span>
        <span class="c"># Rebuild FKs that pointed to us if we previously had to drop them</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">new_field</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">and</span> <span class="n">old_type</span> <span class="o">!=</span> <span class="n">new_type</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">rel</span> <span class="ow">in</span> <span class="n">new_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_all_related_objects</span><span class="p">():</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_fk_sql</span><span class="p">(</span><span class="n">rel</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">rel</span><span class="o">.</span><span class="n">field</span><span class="p">,</span> <span class="s">&quot;_fk&quot;</span><span class="p">))</span>
        <span class="c"># Does it have check constraints we need to add?</span>
        <span class="k">if</span> <span class="n">old_db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_check</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                    <span class="s">&quot;name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_name</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">],</span> <span class="n">suffix</span><span class="o">=</span><span class="s">&quot;_check&quot;</span><span class="p">)),</span>
                    <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                    <span class="s">&quot;check&quot;</span><span class="p">:</span> <span class="n">new_db_params</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">],</span>
                <span class="p">}</span>
            <span class="p">)</span>
        <span class="c"># Drop the default if we need to</span>
        <span class="c"># (Django usually does not use in-database defaults)</span>
        <span class="k">if</span> <span class="n">needs_database_default</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
                <span class="s">&quot;changes&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_no_default</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">column</span><span class="p">),</span>
                <span class="p">}</span>
            <span class="p">}</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
        <span class="c"># Reset connection if required</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">connection_persists_old_columns</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_alter_column_type_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table</span><span class="p">,</span> <span class="n">column</span><span class="p">,</span> <span class="nb">type</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook to specialize column type alteration for different backends,</span>
<span class="sd">        for cases when a creation type is different to an alteration type</span>
<span class="sd">        (e.g. SERIAL in PostgreSQL, PostGIS fields).</span>

<span class="sd">        Should return two things; an SQL fragment of (sql, params) to insert</span>
<span class="sd">        into an ALTER TABLE statement, and a list of extra (sql, params) tuples</span>
<span class="sd">        to run once the field is altered.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">sql_alter_column_type</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">column</span><span class="p">),</span>
                    <span class="s">&quot;type&quot;</span><span class="p">:</span> <span class="nb">type</span><span class="p">,</span>
                <span class="p">},</span>
                <span class="p">[],</span>
            <span class="p">),</span>
            <span class="p">[],</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_alter_many_to_many</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">,</span> <span class="n">strict</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Alters M2Ms to repoint their to= endpoints.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Rename the through table</span>
        <span class="k">if</span> <span class="n">old_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span> <span class="o">!=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">alter_db_table</span><span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">old_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">)</span>
        <span class="c"># Repoint the FK to the other side</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alter_field</span><span class="p">(</span>
            <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
            <span class="c"># We need the field that points to the target model, so we can tell alter_field to change it -</span>
            <span class="c"># this is m2m_reverse_field_name() (as opposed to m2m_field_name, which points to our model)</span>
            <span class="n">old_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">m2m_reverse_field_name</span><span class="p">())[</span><span class="mi">0</span><span class="p">],</span>
            <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">m2m_reverse_field_name</span><span class="p">())[</span><span class="mi">0</span><span class="p">],</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alter_field</span><span class="p">(</span>
            <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
            <span class="c"># for self-referential models we need to alter field from the other end too</span>
            <span class="n">old_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">old_field</span><span class="o">.</span><span class="n">m2m_field_name</span><span class="p">())[</span><span class="mi">0</span><span class="p">],</span>
            <span class="n">new_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">new_field</span><span class="o">.</span><span class="n">m2m_field_name</span><span class="p">())[</span><span class="mi">0</span><span class="p">],</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_create_index_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">column_names</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates a unique name for an index/unique constraint.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># If there is just one column in the index, use a default algorithm from Django</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">column_names</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">suffix</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">truncate_name</span><span class="p">(</span>
                <span class="s">&#39;</span><span class="si">%s</span><span class="s">_</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="n">BaseDatabaseCreation</span><span class="o">.</span><span class="n">_digest</span><span class="p">(</span><span class="n">column_names</span><span class="p">[</span><span class="mi">0</span><span class="p">])),</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">max_name_length</span><span class="p">()</span>
            <span class="p">)</span>
        <span class="c"># Else generate the name for the index using a different algorithm</span>
        <span class="n">table_name</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">,</span> <span class="s">&#39;_&#39;</span><span class="p">)</span>
        <span class="n">index_unique_name</span> <span class="o">=</span> <span class="s">&#39;_</span><span class="si">%x</span><span class="s">&#39;</span> <span class="o">%</span> <span class="nb">abs</span><span class="p">(</span><span class="nb">hash</span><span class="p">((</span><span class="n">table_name</span><span class="p">,</span> <span class="s">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">column_names</span><span class="p">))))</span>
        <span class="n">max_length</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">max_name_length</span><span class="p">()</span> <span class="ow">or</span> <span class="mi">200</span>
        <span class="c"># If the index name is too long, truncate it</span>
        <span class="n">index_name</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">_</span><span class="si">%s%s%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">table_name</span><span class="p">,</span> <span class="n">column_names</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">index_unique_name</span><span class="p">,</span> <span class="n">suffix</span><span class="p">))</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">,</span> <span class="s">&#39;_&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">index_name</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">max_length</span><span class="p">:</span>
            <span class="n">part</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;_</span><span class="si">%s%s%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">column_names</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">index_unique_name</span><span class="p">,</span> <span class="n">suffix</span><span class="p">))</span>
            <span class="n">index_name</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">table_name</span><span class="p">[:(</span><span class="n">max_length</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">part</span><span class="p">))],</span> <span class="n">part</span><span class="p">)</span>
        <span class="c"># It shouldn&#39;t start with an underscore (Oracle hates this)</span>
        <span class="k">if</span> <span class="n">index_name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;_&quot;</span><span class="p">:</span>
            <span class="n">index_name</span> <span class="o">=</span> <span class="n">index_name</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="c"># If it&#39;s STILL too long, just hash it down</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">index_name</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">max_length</span><span class="p">:</span>
            <span class="n">index_name</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">md5</span><span class="p">(</span><span class="n">force_bytes</span><span class="p">(</span><span class="n">index_name</span><span class="p">))</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()[:</span><span class="n">max_length</span><span class="p">]</span>
        <span class="c"># It can&#39;t start with a number on Oracle, so prepend D if we need to</span>
        <span class="k">if</span> <span class="n">index_name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">isdigit</span><span class="p">():</span>
            <span class="n">index_name</span> <span class="o">=</span> <span class="s">&quot;D</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">index_name</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">index_name</span>

    <span class="k">def</span> <span class="nf">_create_index_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="n">sql</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the SQL statement to create the index for one or several fields.</span>
<span class="sd">        `sql` can be specified if the syntax differs from the standard (GIS</span>
<span class="sd">        indexes, ...).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">:</span>
            <span class="n">tablespace_sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">tablespace_sql</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">:</span>
            <span class="n">tablespace_sql</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">tablespace_sql</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">tablespace_sql</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
        <span class="k">if</span> <span class="n">tablespace_sql</span><span class="p">:</span>
            <span class="n">tablespace_sql</span> <span class="o">=</span> <span class="s">&quot; &quot;</span> <span class="o">+</span> <span class="n">tablespace_sql</span>

        <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">column</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">]</span>
        <span class="n">sql_create_index</span> <span class="o">=</span> <span class="n">sql</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_index</span>
        <span class="k">return</span> <span class="n">sql_create_index</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s">&quot;name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_name</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="n">suffix</span><span class="p">)),</span>
            <span class="s">&quot;columns&quot;</span><span class="p">:</span> <span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">),</span>
            <span class="s">&quot;extra&quot;</span><span class="p">:</span> <span class="n">tablespace_sql</span><span class="p">,</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_model_indexes_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return all index SQL statements (field indexes, index_together) for the</span>
<span class="sd">        specified model, as a list.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span> <span class="ow">or</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">proxy</span> <span class="ow">or</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">db_index</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
                <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">field</span><span class="p">],</span> <span class="n">suffix</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">field_names</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">index_together</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field_by_name</span><span class="p">(</span><span class="n">field</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">field_names</span><span class="p">]</span>
            <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_sql</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s">&quot;_idx&quot;</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">output</span>

    <span class="k">def</span> <span class="nf">_create_fk_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">suffix</span><span class="p">):</span>
        <span class="n">from_table</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span>
        <span class="n">from_column</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">column</span>
        <span class="n">to_table</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">related_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span>
        <span class="n">to_column</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">related_field</span><span class="o">.</span><span class="n">column</span>
        <span class="n">suffix</span> <span class="o">=</span> <span class="n">suffix</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;to_table&quot;</span><span class="p">:</span> <span class="n">to_table</span><span class="p">,</span>
            <span class="s">&quot;to_column&quot;</span><span class="p">:</span> <span class="n">to_column</span><span class="p">,</span>
        <span class="p">}</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_fk</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">from_table</span><span class="p">),</span>
            <span class="s">&quot;name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_name</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="p">[</span><span class="n">from_column</span><span class="p">],</span> <span class="n">suffix</span><span class="o">=</span><span class="n">suffix</span><span class="p">)),</span>
            <span class="s">&quot;column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">from_column</span><span class="p">),</span>
            <span class="s">&quot;to_table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">to_table</span><span class="p">),</span>
            <span class="s">&quot;to_column&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">to_column</span><span class="p">),</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_create_unique_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">columns</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sql_create_unique</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s">&quot;name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_index_name</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s">&quot;_uniq&quot;</span><span class="p">)),</span>
            <span class="s">&quot;columns&quot;</span><span class="p">:</span> <span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">column</span><span class="p">)</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">),</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_delete_constraint_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">template</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">template</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&quot;table&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span>
            <span class="s">&quot;name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">quote_name</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_constraint_names</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">column_names</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                          <span class="n">primary_key</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">foreign_key</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                          <span class="n">check</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns all constraint names matching the columns and conditions</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">column_names</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">column_names</span><span class="p">)</span> <span class="k">if</span> <span class="n">column_names</span> <span class="k">else</span> <span class="bp">None</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span> <span class="k">as</span> <span class="n">cursor</span><span class="p">:</span>
            <span class="n">constraints</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">introspection</span><span class="o">.</span><span class="n">get_constraints</span><span class="p">(</span><span class="n">cursor</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">)</span>
        <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">infodict</span> <span class="ow">in</span> <span class="n">constraints</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">column_names</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="n">column_names</span> <span class="o">==</span> <span class="n">infodict</span><span class="p">[</span><span class="s">&#39;columns&#39;</span><span class="p">]:</span>
                <span class="k">if</span> <span class="n">unique</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">infodict</span><span class="p">[</span><span class="s">&#39;unique&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">unique</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">primary_key</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">infodict</span><span class="p">[</span><span class="s">&#39;primary_key&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">primary_key</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">index</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">infodict</span><span class="p">[</span><span class="s">&#39;index&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">index</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">check</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">infodict</span><span class="p">[</span><span class="s">&#39;check&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">check</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">foreign_key</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">infodict</span><span class="p">[</span><span class="s">&#39;foreign_key&#39;</span><span class="p">]:</span>
                    <span class="k">continue</span>
                <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../../index.html">Module code</a>
        
          <ul><li><a href="../../../django.html">django</a>
        
          <ul><li><a href="../../db.html">django.db</a>
        
        <ul><li>django.db.backends.schema</li></ul>
        </li></ul></li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>